package com.dwxt.mall.admin.service;

import com.dwxt.common.base.AdminUserType;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.YesOrNo;
import com.dwxt.common.util.JsonUtils;
import com.dwxt.mall.admin.service.feign.AdminUserService;
import com.dwxt.mall.dao.ProductMapper;
import com.dwxt.mall.entity.Product;
import com.dwxt.mall.entity.ProductCategory;
import com.dwxt.mall.entity.ProductExample;
import com.dwxt.mall.entity.vo.ProductVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceAdmin {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private AdminUserService adminUserService;

    public void createOrUpdateProduct(ProductVO productVO) {
        List<String> covers = productVO.getCovers();
        if (covers != null && covers.size() != 0) {
            String carouselImgUrl = covers.stream().collect(Collectors.joining(","));
            productVO.setCarouselImgUrl(carouselImgUrl);
        }
        if (productVO.getId() == null) {
            productMapper.insertSelective(productVO);
        } else {
            productMapper.updateByPrimaryKeySelective(productVO);
        }
    }

    public BaseResult selectProductList(Integer pageNum, Integer pageSize, Product product) {
        PageHelper.startPage(pageNum, pageSize);
        ProductExample example = new ProductExample();
        example.setOrderByClause("create_time desc");
        ProductExample.Criteria criteria = example.createCriteria();
        if (product.getCategoryId() != null) {
            criteria.andCategoryIdEqualTo(product.getCategoryId());
        }
        if (StringUtils.isNotBlank(product.getName())) {
            criteria.andNameLike("%" + product.getName() + "%");
        }
        if (product.getSupplierId() != null) {
            criteria.andSupplierIdEqualTo(product.getSupplierId());
        }
        if (product.getIsShelf() != null) {
            criteria.andIsShelfEqualTo(product.getIsShelf());
        }
        //配置商城首页返回
        if ("mall".equals(product.getModule())) {
            criteria.andModuleNotEqualTo("场馆培训券").andIsShelfEqualTo(YesOrNo.YES);
        } else if (StringUtils.isNotBlank(product.getModule())) {
            criteria.andModuleEqualTo(product.getModule());
        }

        JSONObject user = JSONObject.fromObject(adminUserService.selectUser().getData());
        if (!user.getString("type").contains(AdminUserType.ADMIN)) {
            criteria.andSupplierIdEqualTo(user.getInt("id"));
        }
        List<Product> productList = productMapper.selectByExampleWithBLOBs(example);
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        //转换成productVO
        productList = getProductVOS(productList);
        pageInfo.setList(productList);

        return BaseResult.ok(pageInfo);
    }

    private List<Product> getProductVOS(List<Product> productList) {
        BaseResult baseResult = adminUserService.selectAllMallUser(0);
        JSONArray jsonArray = JSONArray.fromObject(baseResult.getData());
        HashMap<Integer, String> collect = (HashMap) jsonArray.stream().collect(Collectors.toMap(object -> {
            JSONObject jsonObject = (JSONObject) object;
            return jsonObject.getInt("id");
        }, object -> {
            JSONObject jsonObject = (JSONObject) object;
            return jsonObject.getString("username");
        }));

        return productList.stream().map(pro -> {
            ProductVO productVO = JsonUtils.jsonToPojo(JsonUtils.objectToJson(pro), ProductVO.class);
            productVO.setUsername(collect.get(pro.getSupplierId()));
            productVO.setCovers(Arrays.asList(pro.getCarouselImgUrl().split(",")));
            return productVO;
        }).collect(Collectors.toList());
    }

    public void delete(Integer id) {
        productMapper.deleteByPrimaryKey(id);
    }


    public BaseResult selectIndexList(Product product) {
        List<Product> productList = productMapper.selectIndexList(product);
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        productList = getProductVOS(productList);
        pageInfo.setList(productList);
        return BaseResult.ok(pageInfo);
    }

    public long countByCategoryId(Integer id) {
        ProductExample example = new ProductExample();
        example.createCriteria().andIdEqualTo(id);
        return productMapper.countByExample(example);
    }

    public void changeModuleByCategoryId(ProductCategory productCategory) {
        ProductExample example = new ProductExample();
        example.createCriteria().andCategoryIdEqualTo(productCategory.getId());
        Product product = new Product();
        product.setModule(productCategory.getModule());
        productMapper.updateByExampleSelective(product, example);
    }
}
